/*
This file contains the implementation of common functions from the
corresponding header file.

Copyright (C) 2018  Denis Pankratov

Modifications Copyright (C) 2019 Christodoulos Karavasilis

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include <random>
#include <vector>
#include <set>
#include <cmath>
#include <iostream>
#include "bmcommon.h"
using namespace std;

random_device rd;
mt19937 rng(rd());
uniform_real_distribution<double> rand_frac(0, 1);
random_device rd2;
mt19937 rng2(rd2());

/* Prints the matching for debug purposes. */
void printMatch(vector<int> & online) {
	for (int i = 0; i < (int)online.size(); i++) {
		cout << i << " -> " << online[i] << endl;
	}
}

/*
Writes a sample mean into the mean variable and the unbiased estimator for
the sample standard deviation into std variable. If the number of samples is 1 or less,
writes -1 into both variables.
*/
void getMeanAndStd(const vector<double> &vec, double & mean, double &std) {
	mean = 0.0;
	std = 0.0;
	int n = (int)vec.size();

	// Boundary case
	if (n <= 1) {
		mean = std = -1.0;
		return;
	}

	// Compute MEAN
	for (auto it = vec.begin(); it != vec.end(); it++)
		mean += *it;
	mean /= n;

	// Compute STD
	for (auto it = vec.begin(); it != vec.end(); it++)
		std += (*it - mean)*(*it - mean);

	std /= n - 1;
	std = sqrt(std);
}